//
//  MainFirst_Magazine_TBV_FriedViewCel_WX.m
//  ZITHER
//
//  Created by weixin on 2018/10/19.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "MainFirst_Magazine_TBV_FriedViewCel_WX.h"
#import "Ugc_HomePage_VC_V3.h"
#import "UILabel+YBAttributeTextTapAction.h"
#define UILABEL_LINE_SPACE 6

@implementation MainFirst_Magazine_TBV_FriedViewCel_WX

- (void)awakeFromNib {
    
    [super awakeFromNib];
    
    [self setUI];
    
    [self setMasonry];
    
}

-(void)setMasonry{

    [self.labLike mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(0);
        make.left.equalTo(self).offset(14);
        make.height.mas_equalTo(30);
    }];
    [self.btnLike mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.labLike.mas_centerY).offset(0);
        make.right.equalTo(self).offset(-14);
        make.width.height.mas_equalTo(40);
    }];
    
    [self.labLikeName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labLike.mas_bottom).offset(12);
        make.right.equalTo(self).offset(-14);
        make.left.equalTo(self).offset(14);
    }];
    
    [self.commentNum mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.labLikeName.mas_bottom).offset(50);
        make.right.equalTo(self).offset(-14);
        make.left.equalTo(self).offset(14);
        make.height.height.mas_equalTo(20);
        make.bottom.equalTo(self).offset(-14);
    }];
}

-(void)setModel:(HomePageDetailModelData *)model{
    _model = model;
    
    if ([model.userPgc.laudStatus isEqualToString:@"0"]) {
        self.btnLike.selected = NO;
    }else{
        self.btnLike.selected = YES;
    }
    
    if (![model.userPgc.laudCount isEqualToString:@"0"]) {
        self.labLike.text = [NSString stringWithFormat:@"%@人已赞",model.userPgc.laudCount] ;
    }else{
        self.labLike.text = @"暂无点赞";
    }

    
    
//    NSMutableString *str = [[NSMutableString alloc]init];
//    NSMutableArray *arrM = [[NSMutableArray alloc]init];

    //设置是否有点击效果，默认是YES
    self.labLikeName.enabledTapEffect = NO;
    //    self.labLikeName.text = str;
    
}
-(void)setTotal:(NSString *)total{
    _total = total;
    if (![total isEqualToString:@"0"]) {
        self.commentNum.text = [NSString stringWithFormat:@"%@条评论",total];
    }else{
        self.commentNum.text = @"暂无评论";
    }
}

-(void)setUI{

    
    [self addSubview:self.btnLike];
    [self addSubview:self.labLike];
    [self addSubview:self.labLikeName];
    [self addSubview:self.commentNum];
}


-(QMUIButton *)btnLike{
    if (!_btnLike) {
        _btnLike = [[QMUIButton alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        [_btnLike setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        [_btnLike setImage:[UIImage imageNamed:@"Home_SegCtrl_tracking_active"] forState:UIControlStateNormal];
        [_btnLike setImage:[UIImage imageNamed:@"Home_SegCtrl_tracking_active_p"] forState:UIControlStateSelected];
        @weakify(self);
        
        [[_btnLike rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            if (!self.btnLike.selected) {
                [[serviceDefualt loadupdatePgcLaudPgcId:self.model.userPgc.pgcId laudType:@"1" reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                    if (x.success) {
                        [Tools showTips:@"点赞成功"];
                        self.labLike.text = [NSString stringWithFormat:@"%d人已赞",[self.model.userPgc.laudCount intValue]+1] ;
                        self.btnLike.selected = YES;

                        self.model.userPgc.laudStatus = @"1";
                        self.model.userPgc.laudCount =  [NSString stringWithFormat:@" %ld",[self.model.userPgc.laudCount integerValue]+1];
//                        if (self.likeSuccess) {
//                            self.likeSuccess();
//                        }
//                        [self upLoadData];
                    }else{
                        if (![x.errorInfo isEqualToString:@"未登录"]) {
                            [Tools showTips:x.errorInfo];
                        }else{
//                            [Tools pushSinge];
                        }
                        self.model.userPgc.laudStatus = @"0";
                        self.btnLike.selected = !self.btnLike.selected;
                    }
                }];
            } else {
                [[serviceDefualt loadupdatePgcLaudPgcId:self.model.userPgc.pgcId laudType:@"2" reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                    if (x.success) {
                        [Tools showTips:@"取消成功"];
                        self.model.userPgc.laudStatus = @"0";
                        self.labLike.text = [NSString stringWithFormat:@"%d人已赞",[self.model.userPgc.laudCount intValue]-1] ;
                        self.btnLike.selected = NO;

                        self.model.userPgc.laudCount =  [NSString stringWithFormat:@" %ld",[self.model.userPgc.laudCount integerValue]-1];
//                        if (self.likeSuccess) {
//                            self.likeSuccess();
//                        }
//                        [self upLoadData];
                    }else{
                        if (![x.errorInfo isEqualToString:@"未登录"]) {
                            [Tools showTips:x.errorInfo];
                        }else{
//                            [Tools pushSinge];
                        }
                        self.model.userPgc.laudStatus = @"1";

                        self.btnLike.selected = !self.btnLike.selected;
                    }
                }];
            }
            self.btnLike.selected = !self.btnLike.selected;


        }];
        
    }
    return _btnLike;
}

/** 更新数据 */
-(void)upLoadData{
//    Ugc_Detail_VC_WX *vc = (Ugc_Detail_VC_WX *)[self getViewController:[Ugc_Detail_VC_WX class]];
//    [vc loaddata];
}


-(QMUILabel *)labLike{
    if (!_labLike) {
        _labLike = [[QMUILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        _labLike.numberOfLines = 1;
        _labLike.text = @"暂无点赞";
        _labLike.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    }
    return _labLike;
}
-(QMUILabel *)labLikeName{
    if (!_labLikeName) {
        _labLikeName = [[QMUILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        _labLikeName.numberOfLines = 0;
        _labLikeName.font = [UIFont systemFontOfSize:14];
    }
    return _labLikeName;
}
-(QMUILabel *)commentNum{
    if (!_commentNum) {
        _commentNum = [[QMUILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        _commentNum.numberOfLines = 1;
        _commentNum.text = @"暂无评论";
        _commentNum.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    }
    return _commentNum;
}



-(void)setLabelSpace:(UILabel*)label withValue:(NSString*)str withFont:(UIFont*)font {
    
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    
    paraStyle.lineBreakMode = NSLineBreakByCharWrapping;
    
    paraStyle.alignment = NSTextAlignmentLeft;
    
    paraStyle.lineSpacing = UILABEL_LINE_SPACE; //设置行间距
    
    paraStyle.hyphenationFactor = 1.0;
    
    paraStyle.firstLineHeadIndent = 0.0;
    
    paraStyle.paragraphSpacingBefore = 0.0;
    
    paraStyle.headIndent = 0;
    
    paraStyle.tailIndent = 0;
    
    //设置字间距 NSKernAttributeName:@1.5f
    
    NSDictionary *dic = @{NSFontAttributeName:font, NSParagraphStyleAttributeName:paraStyle, NSKernAttributeName:@1.5f};
    
    NSAttributedString *attributeStr = [[NSAttributedString alloc] initWithString:str attributes:dic];
    
    label.attributedText = attributeStr;
    
}
@end
